#include "queue.h"

void QueueInit(Queue* ph)
{
	assert(ph);
	ph->head = NULL;
	ph->tail = NULL;
}

void QueueDestroy(Queue* ph)
{
	QNode* cur = ph->head;
	while (cur)
	{
		QNode* next = ph->head->next;
		free(cur);
		ph->head= next;
		cur = next;
	}
	ph->head = NULL;
	ph->tail = NULL;
}

void QueuePop(Queue* ph)
{
	assert(ph);
	if (ph->head == NULL)
	{
		return;
	}
	QNode* cur = ph->head->next;
	free(ph->head);
	ph->head = cur;
	if (ph->head == NULL)
	{
		ph->tail = NULL;
	}
}

void QueuePush(Queue* ph, QDataType x)
{
	assert(ph);
	QNode* cur = (QNode*)malloc(sizeof(QDataType));
	if (cur == NULL)
	{
		exit(-1);
	}
	cur->data = x;
	cur->next = NULL;
	if (ph->head == NULL)
	{
		ph->head = cur;
		ph->tail = cur;
	}
	else
	{
		ph->tail->next = cur;
		ph->tail = ph->tail->next;
	}
}

QDataType QueueFront(Queue* ph)
{
	assert(ph);
	if (ph->tail == NULL)
	{
		perror("NULL");
	}
	return ph->head->data;
}

QDataType QueueBack(Queue* ph)
{
	assert(ph);
	if (ph->tail == NULL)
	{
		perror("NULL");
	}
	return ph->tail->data;
}

int QueueSize(Queue* ph)
{
	int a = 1;
	QNode* cur = ph->head;
	if (ph->head == NULL)
	{
		return 0;
	}
	while (cur != ph->tail)
	{
		a++;
	}
	return a;
}

bool QueueEmpty(Queue* ph)
{
	if(ph==NULL)
	return false;
}
